package com.intgrt.manager.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.intgrt.manager.entity.SysOrganize;
import com.intgrt.manager.entity.SysUser;
import com.intgrt.manager.midentity.CombTree;
import com.intgrt.manager.service.SysOrganizeService;
import com.intgrt.manager.service.base.impl.BaseServiceImpl;
import com.intgrt.manager.util.ProjectTools;

@Service
public class SysOrganizeServiceImpl extends BaseServiceImpl implements SysOrganizeService {

	@Override
	public List<Map<String,Object>> getList(SysOrganize entity, String orgid) {
		String sql = "SELECT id,parentid,parentids,orgname,orgphone,orgemail,address,deposit,balance,benefit,remark,crttm"
				+ " FROM sys_organize where";
		if(ProjectTools.isEmpty(entity.getId())){
			sql += " id = "+orgid;
		}else{
			sql += " parentid = "+entity.getId();
		}
		List<Map<String,Object>> reslst = this.getMapList(sql);
		return ProjectTools.getTreeList(reslst, "orgname");
	}

	@Override
	public String saveEntity(SysOrganize entity) {
		List<String> plst = this.getStrField("select parentids from sys_organize where id = "+entity.getParentid(), "parentids");
		String pids = plst.get(0)+entity.getParentid()+",";
		String sql = "insert into sys_organize (parentid,parentids,orgname,orgphone,orgemail,address,remark,crttm)"
				+ " values ("+entity.getParentid()+",'"+pids+"','"+entity.getOrgname()+"','"+entity.getOrgphone()+"','"
				+ entity.getOrgemail()+"','"+ entity.getAddress()+"',"+"'"+entity.getRemark()+"',"
				+ ProjectTools.curTime()+")";
		return this.execute(sql)+"";
	}

	@Override
	public String editEntity(SysOrganize entity) {
		String sql = "update sys_organize set orgname='"+entity.getOrgname()+"',orgphone='"+entity.getOrgphone()+"',"
				+ " orgemail='"+entity.getOrgemail()+"',address='"+entity.getAddress()+"',"
				+ " remark='"+entity.getRemark()+"' where id = "+entity.getId();
		return this.execute(sql)+"";
	}

	@Override
	public String delEntity(String ids) {
		long subnum = this.getCount("sys_organize where parentid = "+ids);
		if(subnum>0){
			return "2";
		}
		String sql = "delete from sys_organize where id = "+ids;
		return this.execute(sql)+"";
	}
	
	@Override
	public List<CombTree> getCombTree(String pid, String orgid) {
		String sql = "";
		List<CombTree> comblst = new ArrayList<CombTree>();
		if(ProjectTools.isEmpty(pid)){
			sql = "select id, orgname from sys_organize where id = "+orgid;
		}else{
			sql = "select id, orgname from sys_organize where parentid = "+pid;
		}
		List<Map<String, Object>> reslst =  this.getMapList(sql);
		if(reslst.size()>0){
			for(Map<String, Object> map:reslst){
				CombTree org = new CombTree(String.valueOf(map.get("id")), String.valueOf(map.get("orgname")));
				comblst.add(org);
			}
		}
		return comblst;
	}

	@Override
	public String recharge(SysUser curuser,String cgemoney,String oprtdesc,String orgid) {
		Map<String,Object> orgMap = this.acqMf(curuser.getOrgid());
		Map<String,Object> orgMap_ps = this.acqMf(orgid);
		if(orgMap == null || orgMap_ps == null)			//代理商查询错误
			return "5";
		int paybalance =Integer.parseInt(String.valueOf((orgMap.get("balance"))));		//执行充值的代理商余额
		int rcgbalance =Integer.parseInt(String.valueOf((orgMap_ps.get("balance"))));		//执行充值的代理商余额
		
		int cashNum = 0;	//充值费用
		try{
			cashNum = Integer.parseInt(cgemoney);
		}catch(Exception e){
			return "2";			//转换数字类型失败
		}
		List<String> sqllst = new ArrayList<String>();
		String curtm = ProjectTools.curTime();
		if(!curuser.getId().equals("1")){
			if(paybalance<cashNum){
				return "3";
			}
			sqllst.add("insert into iot_cashcdr (orgid,befval,midval,aftval,cdrtype,crttm) values ("+curuser.getOrgid()+","
					+ paybalance+","+(-cashNum)+","+(paybalance-cashNum)+",1,"+curtm+")");
			sqllst.add("UPDATE sys_organize set balance="+(paybalance-cashNum)+" WHERE id= "+curuser.getOrgid());
		}
		sqllst.add("insert into iot_cashcdr (orgid,befval,midval,aftval,cdrtype,crttm) values ("+orgid+","
				+ rcgbalance+","+cashNum+","+(rcgbalance+cashNum)+",2,"+curtm+")");
		sqllst.add("UPDATE sys_organize set balance="+(rcgbalance+cashNum)+" WHERE id= "+orgid);
		sqllst.add("insert into sys_oplog (orgid,accid,acccode,optype,content,crttm) values ("+curuser.getOrgid()+","
				+curuser.getId()+",'"+curuser.getAcccode()+"',1,'给代理"+orgid+"充值预付款>>>"+cashNum+",备注信息>>>"+oprtdesc+"',"
				+curtm+")");
		try {
			this.batch(sqllst);
			return "0";
		} catch (Exception e) {
			e.printStackTrace();
			return "7";
		}
	}
	
	private Map<String,Object> acqMf(String orgid){
		String sql="select balance from sys_organize where id = "+orgid;
		List<Map<String, Object>> rlst = this.getMapList(sql);
		Map<String,Object> orgmap = null;
		if(null!=rlst&&rlst.size()>0) {
			orgmap= rlst.get(0);
		}
		return orgmap;
	}

}